package com.auto.tmp;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;

public class FormReader {
	private static String path = "D:/work/GoldMarket/admin/";

	public static Map<String, String> types = Maps.newHashMap();
	static {
		types.put("", "文本输出");
		types.put("input.", "文本框");
		types.put("input.text", "文本框");
		types.put("input.password", "密码框");
		types.put("input.checkbox", "复选框");
		types.put("input.radio", "单选框");
		types.put("input.button", "按钮");
		types.put("textarea", "text area");
		types.put("select.", "列表框");
	}

	public static void readAdmin() throws IOException {
		File[] files = new File(path).listFiles();
		for (File file : files) {
			if (file.getName().endsWith("admin.html")) {
				LineProcessor<List<String>> lineP = new TableLineProcessor();
				Files.readLines(file, Charsets.UTF_8, lineP);
				System.out.println(file.getName());
				List<String> result = lineP.getResult();
				for (String field : result) {
					System.out.println(field);
				}
			}

		}
	}

	public static void readForm() throws IOException {
		File[] files = new File(path).listFiles();
		for (File file : files) {
			if (file.getName().endsWith("_form.html")) {
				System.out.println(file.getName());
				String html = Files.toString(file, Charsets.UTF_8);
				Document doc = Jsoup.parse(html);
				Elements tr = doc.select("tr");
				for (int i = 0; i < tr.size(); ++i) {
					Elements tds = tr.get(i).children();
					Element label = tds.get(0);
					Element input = tds.get(1).child(0);
					String labelName = label.text().replace("*", "")
							.replace(":", "").replace("：", "");
					System.out.println(labelName + "\t" + getType());
					System.out.println(labelName + "条件\t" + getType(input));
				}

			}

		}
	}

	public static String getType() {
		return types.get("");
	}

	public static String getType(Element ele) {
		return types.get(ele.tagName() + "." + ele.attr("type"));
	}

	public static void main(String[] args) throws IOException {
		FormReader.readAdmin();
		FormReader.readForm();

	}
}

class TableLineProcessor implements LineProcessor<List<String>> {
	List<String> result = Lists.newArrayList();

	@Override
	public boolean processLine(String line) throws IOException {
		String lineTrim = line.trim();
		if (lineTrim.startsWith("<th") && lineTrim.endsWith("th>")) {
			int start = lineTrim.indexOf('>') + 1;
			int end = lineTrim.indexOf('<', start);
			result.add(lineTrim.substring(start, end));
		}
		return true;
	}

	@Override
	public List<String> getResult() {
		return result;
	}

}

class FormLineProcessor implements LineProcessor<List<String>> {
	List<String> result = Lists.newArrayList();

	@Override
	public boolean processLine(String line) throws IOException {
		String lineTrim = line.trim();
		if (lineTrim.startsWith("<th") && lineTrim.endsWith("th>")) {
			int start = lineTrim.indexOf('>') + 1;
			int end = lineTrim.indexOf('<', start);
			result.add(lineTrim.substring(start, end));
		}
		return true;
	}

	@Override
	public List<String> getResult() {
		return result;
	}

}
